﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;

namespace IHISCETeim.BLL
{
    public class VINController:BaseController
    {

        /// <summary>
        /// 0为VIN代码对应值，1为代码位置对应的加权系数，2为年份对应的代码
        /// </summary>
        /// <param name="ConfigType"></param>
        /// <returns></returns>
        public static List<VinConfigInfo> VinConfig_GetAllByType(int ConfigType)
        {
            List<VinConfigInfo> _list = DC.Get<List<VinConfigInfo>>(GetDataCacheKey(AllVinConfigKey, ConfigType));
            if (_list == null)
            {
                _list =CBO.FillCollection<VinConfigInfo>( VINDAL.VinConfig_GetAllByType(ConfigType));
                DC.Add(GetDataCacheKey(AllVinConfigKey, ConfigType), _list);
            }

            return _list;
        }
        public static List<VinMacInfo> VinMac_GetAll()
        {
            List<VinMacInfo> _list = DC.Get<List<VinMacInfo>>(AllVinMacKey);
            if (_list == null)
            {
                _list = CBO.FillCollection<VinMacInfo>(VINDAL.VinMac_GetAll());
                DC.Add(AllVinMacKey, _list);
            }
            return _list;
        }
       


        #region 环保代码生成
        /// <summary>
        /// 生成VIN
        /// </summary>
        /// <param name="SerialNumber"></param>
        /// <param name="obj"></param>
        /// <returns>
        /// 0为成功，其它都失败
        /// </returns>
        public static string CreateVin(string SerialNumber, VinHistoryInfo obj)
        {
            // returnVin = "";
            if (SerialNumber.Length < 6)
                return "机号错误,不足6位";
            OJobInfo _ojob = OJobController.GetBySerialNumber(SerialNumber.Trim().ToUpper());//
            //if (string.IsNullOrEmpty(_ojob.EngineNumber))
            //    return "没有填写相应的发动机号，无法生成VIN";

            string _SerialNumber = SerialNumber.Substring(SerialNumber.Length - 6, 6);//取后6个字符
            //前5位固定，公司代码加上燃油类型
            string VIN = "L3211";
            VinMacInfo _vinmacOjb = VinMac_GetAll().Find(e => e.MacType.ToUpper().Trim() == _ojob.MachineType.ToUpper().Trim());
            if(_vinmacOjb==null)
                return "机号对应的机型与VIN系统中的机型不符，无法生成VIN";
            //第6到8位
            string vin68= _vinmacOjb.TuYinCode;           
            VIN = VIN + vin68;
            //第9位为检验位，先留空(用-代替)，后续再处理
            VIN = VIN + "-";
            //第10位 为生产年份对应位
            //if (string.IsNullOrEmpty(obj.SCDATE))
            //     return "没有对应的出厂日期，无法获取对应的生成年份";
            string vin10 =VinConfig_GetAllByType(2).Find(e => e.ConfigKey.Trim().ToUpper() == System.DateTime.Now.Year.ToString().ToUpper()).ConfigValue;
            
            VIN = VIN + vin10;
            //第11位，排放位
            VIN= VIN+_vinmacOjb.EmissionStage;
            //第12－17位为机号后6位
            VIN = VIN + _SerialNumber;

            /////////////处理第9位，检验位////////////////////////////////////////
            int sumall = 0;
            int index = 1;
            Char[] chArr = VIN.ToCharArray();
            foreach (var c in chArr)
            {
                if (c.ToString() != "-")
                {
                    string _ConfigValue = VinConfig_GetAllByType(0).Find(e => e.ConfigKey.Trim().ToUpper() == c.ToString().Trim().ToUpper()).ConfigValue;
                    string _ConfigValue2 = VinConfig_GetAllByType(1).Find(e => e.ConfigKey.Trim().ToUpper() == index.ToString().Trim().ToUpper()).ConfigValue;
                    sumall += Convert.ToInt32(_ConfigValue) * Convert.ToInt32(_ConfigValue2);
                }
                index++;
            }

            string VIN9=Convert.ToInt32(sumall % 11).ToString();
            VIN9 = VIN9 == "10" ? "X" : VIN9;

            VIN = VIN.Replace("-", VIN9);
            // returnVin = VIN;
            obj.VIN = VIN;
            obj.MacType = _ojob.MachineType;
            obj.SerialNumber = _ojob.SerialNumber;
            //obj.FDJH = _ojob.EngineNumber;
            obj.XXGKH = _vinmacOjb.XXGKH;
            obj.SCCDZ = _vinmacOjb.SCCDZ;
           // obj.SCDATE =Convert.ToDateTime(_ojob.OnlineDate).ToString("yyyy-MM-dd");
            obj.CCDATE = "";
            obj.GKWWW = "http://www.ihisce.com";
            obj.FDJSB = _vinmacOjb.FDJSB;
            obj.FDJSCCDZ = _vinmacOjb.FDJSCCDZ;
            return "";
        }
        #endregion

        public static int Add(VinHistoryInfo obj)
        {
            return VINDAL.Add(obj);
        }

        public static int Update(VinHistoryInfo obj)
        {
            return VINDAL.Update(obj);
        }

        public static bool Delete(int id)
        {
            return VINDAL.Delete(id);
        }
        public static VinHistoryInfo GetList(int ID)
        {
            VinHistoryInfo _obj = CBO.FillObject< VinHistoryInfo>( VINDAL.GetList(ID));
            if (_obj == null)
                _obj = new VinHistoryInfo() { ID = 0 };

            return _obj;
        }
        public static VinHistoryInfo GetListByVin(string VIN)
        {
            VinHistoryInfo _obj= CBO.FillObject<VinHistoryInfo>(VINDAL.GetListByVin(VIN));
            if (_obj == null)
                _obj = new VinHistoryInfo() {ID=0 };
            return _obj;
        }

        public static VinHistoryInfo GetListBySer(string Ser)
        {
            VinHistoryInfo _obj = CBO.FillObject<VinHistoryInfo>(VINDAL.GetListBySer(Ser));
            if (_obj == null)
                _obj = new VinHistoryInfo() { ID = 0 };
            return _obj;
        }

        public static DataTable GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return VINDAL.GetPagination(whereSql, PageIndex, PageSize, Sort, ref TotalCount);
        }
        
    }
}
